package org.Component;

import com.google.gson.Gson;
import org.JsonMsg.PersonalMsg;
import org.JsonMsg.Post;
import org.Panel.MainPanel;
import org.Tools.OnLineOP;
import org.implement.Panel.PanelSize;
import org.implement.Panel.PanelSkip;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

//帖子主页中的单条帖子,包含标题,作者,日期,对应的向服务器端请求完整内容的标识
public class PostBar extends JPanel {
    String PostTitle;   //标题
    JLabel PostTitleLabel;
    String PostDate;    //日期
    JLabel PostDateLabel;
    String PostAuthor;  //作者
    JLabel PostAuthorLabel;
    String PostContent; //内容
    JLabel PostContentLabel;

    //服务器请求语句,向服务器请求帖子全部内容
    String jsonMsg;

    //母界面接口
    PanelSize panelSize;
    PanelSkip panelSkip;

    Gson gson = new Gson();

    //回复窗口折叠按钮
    JLabel unfold = new JLabel("回复");

    public PostBar(PanelSize panelSize, PanelSkip panelSkip, Post post) {

        this.panelSize = panelSize;
        this.panelSkip = panelSkip;

        PostTitle = post.getTitle();
        PostDate = post.getUpdatedAt();
        //获取作者信息
        String msg;
        String jsonmsg;
        OnLineOP onLineOP = new OnLineOP();

        msg = "getUserIdByUser-"+post.getUserId()+"-svc";
        try {
            jsonmsg = onLineOP.sendone(msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        PersonalMsg personalMsg = new PersonalMsg();//个人信息类
        personalMsg = gson.fromJson(jsonmsg, PersonalMsg.class);
        String username = personalMsg.getUsername();
        PostAuthor = username;

        //属性设置
        setLayout(null);
        setBounds(0,0,600,100);

        //标题
        PostTitleLabel = new JLabel(PostTitle);
        PostTitleLabel.setBounds(5,5,getWidth(),50);
        //点击标题进入帖子
        PostTitleLabel.addMouseListener(new MouseAdapter() {
            //鼠标移入标识
            int f=1;
            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    MainPanel.post = post;
                    //点击后进行跳转
                    panelSkip.skip(5);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                //改变鼠标移入标识
                f=1;
                //鼠标移动至文本中,改变鼠标指针样式
                PostTitleLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                //将文字颜色改为蓝色
                PostTitleLabel.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
                //当鼠标移出文本时,回复文字颜色黑色
                PostTitleLabel.setForeground(Color.BLACK);
            }
        });

        //字体
        Font fon = new Font(null,0,25);
        PostTitleLabel.setFont(fon);

        //作者
        PostAuthorLabel = new JLabel(PostAuthor);
        PostAuthorLabel.setBounds(5,60,100,30);
        PostAuthorLabel.setForeground(Color.GRAY);

        //日期
        PostDateLabel = new JLabel(PostDate);
        PostDateLabel.setBounds(250,60,200,30);
        PostDateLabel.setForeground(Color.GRAY);

        //内容
//        PostContentLabel = new JLabel(PostContent);

        //添加至界面
        add(PostTitleLabel);
        add(PostDateLabel);
        add(PostAuthorLabel);
//        add(PostContentLabel);

    }


}
